import { StepEngine } from '../consts/stepEngine'
import { getAjaxsOfIds, getMocksOfIds, getElementsOfIds, getAllConfigs } from '../source/getData'

// 拉基础数据
export const getStepListBasicDataAsync = async (stepList) => {
    // 包括 ajax, mock, element, config 基础信息需要拉
    const refIdMap = { ajax: new Set(), mock: new Set(), element: new Set() }
    stepList
        .map(step => StepEngine[step.code].getRefIdMap(step))
        .forEach(map => {
            Object.keys(refIdMap).forEach(key => map[key] && refIdMap[key].add(map[key]))
        })

    const getAjaxDataPromise = refIdMap.ajax.size ? getAjaxsOfIds(Array.from(refIdMap.ajax)) : Promise.resolve([])
    const getMockDataPromise = refIdMap.mock.size ? getMocksOfIds(Array.from(refIdMap.mock)) : Promise.resolve([])
    const getElementDataPromise = refIdMap.element.size ? getElementsOfIds(Array.from(refIdMap.element)) : Promise.resolve([])
    const getConfigPromise = getAllConfigs()

    const getAllBasicDataPromise = Promise.all([getAjaxDataPromise, getMockDataPromise, getElementDataPromise, getConfigPromise])
    const [ajax, mock, element, config] = await getAllBasicDataPromise

    return { ajax, mock, element, config }
}